package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.feign.item.ItemFeignClient;
import com.atguigu.gmall.feign.product.ProductFeignClient;
import com.atguigu.gmall.model.to.BaseCategroysTo;

import com.atguigu.gmall.model.to.SkuDetailTo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author A
 * @version 1.0
 * @description: TODO
 * @date 2024/9/10 3:33
 */
@Slf4j
@Controller
public class WebAllController {
    @Autowired
    ProductFeignClient productFeignClient;
    @Autowired
    ItemFeignClient itemFeignClient;
    @GetMapping({"/","index.html"})
    public String index(Model model){

        Result<List<BaseCategroysTo>> categorys = productFeignClient.getCategorys();

        if (categorys.isOk()){
            List<BaseCategroysTo> data = categorys.getData();
            model.addAttribute("list",data);
        }
        return "index/index";
    }
    @GetMapping("/{skuId}.html")
    public String itemPage(@PathVariable("skuId") Long skuId,Model model){
        log.info("商品详情: {}",skuId);
        //查询数据

        //1.当前sku所在的分类的完整信息
        SkuDetailTo skuDetail = itemFeignClient.getSkuDetail(skuId);

        model.addAttribute("categoryView",skuDetail.getCategoryView());
        //2.当前sku基本信息
        model.addAttribute("skuInfo",skuDetail.getSkuInfo());
        //3.查询sku的实时价格
        Result<BigDecimal> price = productFeignClient.getPrice(skuId);
        model.addAttribute("price",price.getData());
        //4.sku的spu的所有销售属性集合
        model.addAttribute("spuSaleAttrList",skuDetail.getSpuSaleAttrList());
        //5.查实际sku组合
        model.addAttribute("valuesSkuJson",skuDetail.getValuesSkuJson());
        return "item/index";
    }
}
